/**
 *
 */
package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.DeliveryAdapter;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类名称：DeliveryRecordActivity 类描述： 创建人：何琳 创建时间：2015年7月28日 下午4:34:49
 */
public class DeliveryRecordAct extends BaseActivity {

    @Bind(R.id.delivery_img)
    ImageView deliveryImg;
    @Bind(R.id.delivery_proname)
    TextView deliveryProname;
    @Bind(R.id.delivery_orderid)
    TextView deliveryOrderid;
    @Bind(R.id.delivery_proid)
    TextView deliveryProid;
    private ListView listview;
    private DeliveryAdapter adapter;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    private LayoutInflater lif;
    private Context context = DeliveryRecordAct.this;
    private String bulkOrderId, productId, productname, imgUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_delivery_record);
        ButterKnife.bind(this);
        initBar();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("交付记录");
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        tabBack();
        initView();
        loadDelivery();
        initData();
    }

    private void initView() {
        bulkOrderId = getIntent().getStringExtra("bulkOrderId");
        productId = getIntent().getStringExtra("productId");
        productname = getIntent().getStringExtra("productName");
        imgUrl = getIntent().getStringExtra("mainImage");
        deliveryProname.setText(productname);
        deliveryProid.setText(productId);
        deliveryOrderid.setText(bulkOrderId);
        if (!TextUtils.isEmpty(imgUrl)) {
            if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                imgUrl = "http://" + imgUrl;
            }
        }
        Glide.with(context)
                .load(imgUrl)
                .centerCrop()
                .into(deliveryImg);
        listview = (ListView) findViewById(R.id.delivery_listview);
        adapter = new DeliveryAdapter(this, dataList);
        lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void loadDelivery() {

        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                if (Tool.getString(result, "resultCode").equals("0")) {
                    JSONArray array = Tool.getJsonArray(result, "datalist");
                    dataList = Tool.getListMapByJsonArray(array);
                    adapter.update(dataList);
//                    deliveryProname.setText();
                }else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostDelivery(context, bulkOrderId, ioAuthCallBack);
    }

    private void initData() {

        View headerView = lif.inflate(R.layout.header_deliveryrecord, null);
        listview.addHeaderView(headerView);
        listview.setAdapter(adapter);
    }

//    protected void onResume() {
//        loadDelivery();
//        super.onResume();
//    }
}
